import { createRoom, getRoomList, getRoomMsg, getRoomDetail } from '@/api/chat'
const { VITE_CONVERSATION_LIMIT, VITE_DEFAULT_AI_MODEL } = import.meta.env;

export default {
    namespaced: true,
    state: () => ({
        currentRoomId: null,
        roomList: [],
        history: [],
        isGenerating: false,
        generatingMessage: '',
        hasNextPage: false,
        roomDetail: null,
        currentPage: 0,
        isGeneratingImage: false,
        chatMode: VITE_DEFAULT_AI_MODEL,
        refreshConversation: undefined 
    }),
    mutations: {
        setIsGeneratingImage(state, val) {
            state.isGeneratingImage = val
            if(!val) {
              state.refreshConversation = undefined
            }
        },
        setCurrentRoomId(state, roomId) {
            state.currentRoomId = roomId
        },
        setRoomList(state, roomList) {
            state.roomList = roomList
        },
        setHistory(state, history) {

            state.history = history
        },
        pushMessage(state, message) {
            state.history = [message,...state.history]
        },
        setIsGenerating(state, val) {
            state.isGenerating = val
            if(!val) {
              state.refreshConversation = undefined
            }
        },
        setGeneratingMessage(state, msg) {
            state.generatingMessage = msg
        },
        appendGeneratingMessage(state, msg) {
            state.generatingMessage += msg
        },
        setHasNextPage(state, val) {
            state.hasNextPage = val
        },
        setRoomDetail(state, detail) {
            state.roomDetail = detail
        },
        resetRoom(state) {
            state.currentRoomId = null
            state.history = []
            state.isGenerating = false
            state.generatingMessage = ''
            state.hasNextPage = false
            state.roomDetail = null
        },
        setRefreshConversation (state ,value) {
          state.refreshConversation = value;
        }
    },
    actions: {
        createRoom: async (context, version) => {
            const resp = await createRoom(version)
            if (!resp.status && resp) {
                context.commit('setCurrentRoomId', resp)
                return resp
            }
        },
        updateRoomList: async (context) => {
            const resp = await getRoomList({
                page: 0,
                size: VITE_CONVERSATION_LIMIT,
            })
            context.commit('setRoomList', resp?.content ?? [])
        },
        updateRoomDetail: async (context) => {
            const resp = await getRoomDetail(context.state.currentRoomId)
            resp.data && context.commit('setRoomDetail', resp.data)
        },
        setRoomChatHistory: async (context, roomId) => {
            const resp = await getRoomMsg({
                roomId,
                page: 0,
            })
            const history =
                resp.content?.map((item) => {
                    return {
                      ...item,
                        role: item.source === 1 ? 'user' : 'assistant',
                        message: item.context,
                        files: item.fileList,
                    }
                }) ?? []
            if (resp.totalElements > history.length) {
                context.commit('setHasNextPage', true)
            } else {
                context.commit('setHasNextPage', false)
            }
            context.commit('setHistory', history)
        },
        fetchHistoryNextPage: async (context, payload) => {
            const { roomId, page } = payload
            const resp = await getRoomMsg({
                roomId,
                page,
            })
            const history =
                resp.content?.map((item) => {
                    return {
                      ...item,
                        role: item.source === 1 ? 'user' : 'assistant',
                        message: item.context,
                        files: item.fileList,
                    }
                }) ?? []
            const newHistory = [...context.state.history, ...history]
            if (resp.totalElements > newHistory.length) {
                context.commit('setHasNextPage', true)
            } else {
                context.commit('setHasNextPage', false)
            }
            context.commit('setHistory', newHistory)
            return resp
        },

        setRefreshConversation(context, payload) {
          context.commit('setRefreshConversation', payload)
        }
    },
}
